package com.shuai.chatroom.controller;

import com.shuai.chatroom.common.AppConstant;
import com.shuai.chatroom.common.HttpResult;
import com.shuai.chatroom.entity.UserInfo;
import com.shuai.chatroom.entity.vo.FriendInfoVO;
import com.shuai.chatroom.exception.ControllerException;
import com.shuai.chatroom.service.FriendService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/friend")
public class FriendController {
    @Resource
    private FriendService friendService;

    @RequestMapping("/list")
    public HttpResult getFriendList(HttpServletRequest request) throws ControllerException {
        HttpSession session = request.getSession(false);
        if (session == null
                || session.getAttribute(AppConstant.USER_SESSION_KEY) == null) {
            throw new ControllerException("非法请求");
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(AppConstant.USER_SESSION_KEY);
        if (userInfo.getUId() == null) {
            throw new ControllerException("参数丢失");
        }
        List<FriendInfoVO> friendList = friendService.getFriendList(userInfo.getUId());
        System.out.println(friendList);
        return HttpResult.success(friendList);
    }
}
